import pymysql
import urllib.request
import json
from datetime import date, datetime, timedelta

db = pymysql.connect("114.115.160.16","root","123456Aa!","fund")
cursor = db.cursor()

headers = {
    "token":"atTPd9c8sA"
}
idid = 1
rate_id = 1
sql = "select fund_id,category from api_fund"
cursor.execute(sql)
size_id = 751
for data in cursor.fetchall():
    fund_id = data[0]
    category = data[1]
    print(fund_id)
    response = urllib.request.urlopen("https://api.doctorxiong.club/v1/fund/detail" + "?code=" + fund_id + "&startDate=2021-05-12" +"&token=atTPd9c8sA").read().decode("UTF-8")
    if category != 5:
        continue
    try:
        obj = json.loads(response)["data"]
      #  print(obj)
        fund_scale = obj["fundScale"]
        date = "2021-05-13"
        sql = "insert into api_size values(%d,'%s','%s','%s')" % (size_id,fund_id,fund_scale,date)
        #print(sql)
        cursor.execute(sql)
        print("ok!",fund_id)
        size_id += 1
    except Exception as e:
        print(e)
        print("fail!",response)
db.commit()